// /** 存储类，用于存储需要本地保存的数据 */

import { _decorator, sys } from 'cc';
interface ShuZiHuaRongDao_GameStorageData {
    game_mode_3x3: any;
    game_mode_4x4: any;
    game_mode_5x5: any;
    game_mode_6x6: any;
    game_mode_7x7: any;
    game_mode_8x8: any;
    game_mode_9x9: any;
    game_mode_10x10: any;
}

export class ShuZiHuaRongDao_GameStorage {
    static data: ShuZiHuaRongDao_GameStorageData = null;
    static defaultValue: ShuZiHuaRongDao_GameStorageData = null;
    static initData() {
        ShuZiHuaRongDao_GameStorage.InitDefaultData();

        let dataStr = sys.localStorage.getItem(`ShuZiHuaRongDao_GameStorage`);
        if (!dataStr || dataStr === "") {
            ShuZiHuaRongDao_GameStorage.clearData();
        } else {
            ShuZiHuaRongDao_GameStorage.data = JSON.parse(dataStr);
            ShuZiHuaRongDao_GameStorage.saveData();
        }

        ShuZiHuaRongDao_GameStorage.SetEveryData();
    }
    //    // 初始化默认值
    static InitDefaultData() {
        ShuZiHuaRongDao_GameStorage.defaultValue = {
            game_mode_3x3: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_4x4: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_5x5: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_6x6: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_7x7: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_8x8: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_9x9: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
            game_mode_10x10: {
                score: 0,
                best_time: -1,
                step_number: 0,
                current_data: {
                    map: [],
                    time: 0,
                    step_number: 0,
                }
            },
        };
    }
    static SetEveryData() {
        // 递归检查并更新所有属性
        ShuZiHuaRongDao_GameStorage.mergeDataRecursively(ShuZiHuaRongDao_GameStorage.data, ShuZiHuaRongDao_GameStorage.defaultValue);
        ShuZiHuaRongDao_GameStorage.saveData();
    }
    //    // 递归合并数据，确保所有属性都存在
    static mergeDataRecursively(target: any, source: any) {
        for (let key in source) {
            if (source.hasOwnProperty(key)) {
                // 如果目标对象没有这个属性，直接赋值
                if (!target.hasOwnProperty(key)) {
                    target[key] = source[key];
                }
                // 如果目标对象有这个属性，且源对象的值是对象类型，则递归检查
                else if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
                    if (typeof target[key] === 'object' && target[key] !== null && !Array.isArray(target[key])) {
                        ShuZiHuaRongDao_GameStorage.mergeDataRecursively(target[key], source[key]);
                    } else {
                        // 如果目标不是对象类型，用默认值替换
                        target[key] = source[key];
                    }
                }
                // 对于数组类型，如果目标没有这个属性，则使用默认值
                else if (Array.isArray(source[key]) && !target.hasOwnProperty(key)) {
                    target[key] = source[key];
                }
            }
        }
    }
    static clearData() {
        ShuZiHuaRongDao_GameStorage.data = { ...ShuZiHuaRongDao_GameStorage.defaultValue };
    }
    static saveData() {
        let dataStr = JSON.stringify(ShuZiHuaRongDao_GameStorage.data);
        sys.localStorage.setItem(`ShuZiHuaRongDao_GameStorage`, dataStr);
    }
}